<a href='https://github.com/angular/angular.js/edit/v1.4.x/src/ngMock/angular-mocks.js?message=docs(angular.mock.inject)%3A%20describe%20your%20change...#L2301' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit">&nbsp;</i>Improve this Doc</a>



<a href='https://github.com/angular/angular.js/tree/v1.4.7/src/ngMock/angular-mocks.js#L2301' class='view-source pull-right btn btn-primary'>
  <i class="glyphicon glyphicon-zoom-in">&nbsp;</i>View Source
</a>


<header class="api-profile-header">
  <h1 class="api-profile-header-heading">angular.mock.inject</h1>
  <ol class="api-profile-header-structure naked-list step-list">
    
    <li>
      - function in module <a href="api/ngMock">ngMock</a>
    </li>
  </ol>
</header>



<div class="api-profile-description">
  <p><em>NOTE</em>: This function is also published on window for easy access.<br>
<em>NOTE</em>: This function is declared ONLY WHEN running tests with jasmine or mocha</p>
<p>The inject function wraps a function into an injectable function. The inject() creates new
instance of <a href="api/auto/service/$injector">$injector</a> per test, which is then used for
resolving references.</p>
<h2 id="resolving-references-underscore-wrapping-">Resolving References (Underscore Wrapping)</h2>
<p>Often, we would like to inject a reference once, in a <code>beforeEach()</code> block and reuse this
in multiple <code>it()</code> clauses. To be able to do this we must assign the reference to a variable
that is declared in the scope of the <code>describe()</code> block. Since we would, most likely, want
the variable to have the same name of the reference we have a problem, since the parameter
to the <code>inject()</code> function would hide the outer variable.</p>
<p>To help with this, the injected parameters can, optionally, be enclosed with underscores.
These are ignored by the injector when the reference name is resolved.</p>
<p>For example, the parameter <code>_myService_</code> would be resolved as the reference <code>myService</code>.
Since it is available in the function body as <em>myService</em>, we can then assign it to a variable
defined in an outer scope.</p>
<pre><code>// Defined out reference variable outside
var myService;

// Wrap the parameter in underscores
beforeEach( inject( function(_myService_){
  myService = _myService_;
}));

// Use myService in a series of tests.
it(&#39;makes use of myService&#39;, function() {
  myService.doStuff();
});
</code></pre>
<p>See also <a href="api/ngMock/function/angular.mock.module">angular.mock.module</a></p>
<h2 id="example">Example</h2>
<p>Example of what a typical jasmine tests looks like with the inject method.</p>
<pre><code class="lang-js">angular.module(&#39;myApplicationModule&#39;, [])
    .value(&#39;mode&#39;, &#39;app&#39;)
    .value(&#39;version&#39;, &#39;v1.0.1&#39;);


describe(&#39;MyApp&#39;, function() {

  // You need to load modules that you want to test,
  // it loads only the &quot;ng&quot; module by default.
  beforeEach(module(&#39;myApplicationModule&#39;));


  // inject() is used to inject arguments of all given functions
  it(&#39;should provide a version&#39;, inject(function(mode, version) {
    expect(version).toEqual(&#39;v1.0.1&#39;);
    expect(mode).toEqual(&#39;app&#39;);
  }));


  // The inject and module method can also be used inside of the it or beforeEach
  it(&#39;should override a version and test the new version is injected&#39;, function() {
    // module() takes functions or strings (module aliases)
    module(function($provide) {
      $provide.value(&#39;version&#39;, &#39;overridden&#39;); // override version here
    });

    inject(function(version) {
      expect(version).toEqual(&#39;overridden&#39;);
    });
  });
});
</code></pre>

</div>




<div>
  

    

  <h2 id="usage">Usage</h2>
    
      <p><code>angular.mock.inject(fns);</code></p>


    

    
<section class="api-section">
  <h3>Arguments</h3>

<table class="variables-matrix input-arguments">
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Details</th>
    </tr>
  </thead>
  <tbody>
    
    <tr>
      <td>
        fns
        
        
      </td>
      <td>
        <a href="" class="label type-hint type-hint-object">...Function</a>
      </td>
      <td>
        <p>any number of functions which will be injected using the injector.</p>

        
      </td>
    </tr>
    
  </tbody>
</table>

</section>
    
    

  
  
  



  
</div>


